﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;

namespace SolidworksDLL
{
    public partial class TestForm1 : Form
    {
        public TestForm1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwApp();
            if (b)
            {

            }
            else
            {
                MessageBox.Show("获取失败");
                return;
            }

            ModelDoc2 swModel = SolidWorksHelp.SwApp.ActiveDoc;
            //string result = ModelDocHelp.GetActiveDoc();
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败" );
                return;
            }
            //Configuration cfgr = ConfigurationHelp.GetCurCfg(swModel);
            string cfgName = swModel.GetActiveConfiguration().Name;
            string th = swModel.CustomInfo2[cfgName, "图号1"];     //获得自定义属性的值
            //MessageBox.Show(cfgName);
            MessageBox.Show(th);
            swModel.DeleteCustomInfo2(cfgName, "图号1");          //删除自定义属性
            swModel.AddCustomInfo3(cfgName, "图号1", 30, "123456");//只有不存在的属性名称才会生效
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwApp();
            if (b)
            {

            }
            else
            {
                MessageBox.Show("获取失败");
                return;
            }

            ModelDoc2 swModel = SolidWorksHelp.SwApp.ActiveDoc;
            //string result = ModelDocHelp.GetActiveDoc();
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败");
                return;
            }
            Dictionary<string, string> ptyDict = new Dictionary<string, string>();
            CustomPryHelp.GetAllPryDict(swModel, "", ref ptyDict);

            List<Configuration> configurations = ConfigurationHelp.GetAllCfg(swModel);
            listBox1.Items.Clear();
            foreach (var item in configurations)
            {
                //CustomPryHelp.ClearPry(swModel, item.Name);
                //CustomPryHelp.AddPryDict(swModel, item.Name, ptyDict);

                listBox1.Items.Add(item.Name);

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwApp();
            if (b)
            {

            }
            else
            {
                MessageBox.Show("获取失败");
                return;
            }

            ModelDoc2 swModel = SolidWorksHelp.SwApp.ActiveDoc;
            //string result = ModelDocHelp.GetActiveDoc();
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败");
                return;
            }

            string cfgName;
            if (null == listBox1.SelectedItem )
            {
                cfgName = "";
            }
            else
            {
                cfgName = listBox1.SelectedItem.ToString();
            }
           
            Dictionary<string, string> ptyDict = new Dictionary<string, string>();
            CustomPryHelp.GetAllPryDict(swModel, cfgName, ref ptyDict);                    //获得所有自定义属性值
            //List<Configuration> configurations = ConfigurationHelp.GetAllCfg(swModel);
            //foreach (var item in configurations)
            //{

            //}
           
            dataGridView1.Rows.Clear();
            int index = 0;
            foreach (var item in ptyDict)
            {

                dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = item.Key;
                dataGridView1.Rows[index].Cells[1].Value = item.Value;
                index = index + 1;
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwApp();
            if (b)
            {

            }
            else
            {
                MessageBox.Show("获取失败");
                return;
            }

            ModelDoc2 swModel = SolidWorksHelp.SwApp.ActiveDoc;
            //string result = ModelDocHelp.GetActiveDoc();
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败");
                return;
            }

            string cfgName;
            if (null == listBox1.SelectedItem)
            {
                cfgName = "";
            }
            else
            {
                cfgName = listBox1.SelectedItem.ToString();
            }
            Dictionary<string, string> ptyDict = new Dictionary<string, string>();
            int tatolRowCount = dataGridView1.RowCount - 1;

            for (int i = 0; i < tatolRowCount - 1; i++)
            {
                if ((string) dataGridView1.Rows[i].Cells[0].Value != "")
                { 
                string name = (string) dataGridView1.Rows[i].Cells[0].Value;
                string value = (string)dataGridView1.Rows[i].Cells[1].Value;
                ptyDict[name] = value;
                }
            }
            CustomPryHelp.AddPryDict(swModel, cfgName, ptyDict);
            MessageBox.Show("写入完成");

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwApp();
            if (b)
            {

            }
            else
            {
                MessageBox.Show("获取失败");
                return;
            }

            ModelDoc2 swModel = SolidWorksHelp.SwApp.ActiveDoc;
            //string result = ModelDocHelp.GetActiveDoc();
            if (null == swModel)
            {
                MessageBox.Show("打开模型失败");
                return;
            }

            string cfgName;
            if (null == listBox1.SelectedItem)
            {
                cfgName = "";
            }
            else
            {
                cfgName = listBox1.SelectedItem.ToString();
            }

            Dictionary<string, string> ptyDict = new Dictionary<string, string>();
            CustomPryHelp.GetAllPryDict(swModel, cfgName, ref ptyDict);                    //获得所有自定义属性值
   
            dataGridView1.Rows.Clear();
            int index = 0;
            foreach (var item in ptyDict)
            {

                dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = item.Key;
                dataGridView1.Rows[index].Cells[1].Value = item.Value;
                index = index + 1;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            listBox1.Items.Clear();
        }
    }
}
